package com.daviteq.ems.utils;

import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.log4j.Logger;

/**
 * @author thanhthi.tran
 * @copyright 2014 DAVITEQ, All rights reserved
 */
public class EMSConfig {

    private final static Logger log = Logger.getLogger(EMSConfig.class);
    
    private static EMSConfig instance = null;

   // E:/ThanhUy/Coding/MyWorkSpace/DAO/DAO/src/main/webapp/META-INF
    public final static String GLOBAL_CFG_PATH = 
            "E:/ThanhUy/Coding/MyWorkSpace/DAO/DAO/src/main/webapp/META-INF/cassandra.properties";

    private EMSConfig() {
    }

    private static class Loader {

        private static final EMSConfig INSTANCE = new EMSConfig();
    }

    public static EMSConfig getInstance() {
        return Loader.INSTANCE;
    }
    
    public CassandraConfig loadGlobalConfig() throws FileNotFoundException,
            IOException {

        PropertiesUtil gloCfg = new PropertiesUtil(GLOBAL_CFG_PATH);

        CassandraConfig cfg = new CassandraConfig();

        cfg.setCluster(gloCfg.get(CassandraConfig.CLUSTER_NAME));
        cfg.setKeyspace(gloCfg.get(CassandraConfig.KEYSPACE));
        cfg.setNode(gloCfg.get(CassandraConfig.NODE));
        
        try {
            cfg.setPort(Integer.parseInt(gloCfg.get(CassandraConfig.PORT)));
        } catch (Exception ex) {
            log.error("the port is invalid format");
            log.info("set default " + CassandraConfig.PORT + ": 9042");
            cfg.setPort(9042);
        }
        
        try {
            cfg.setCoreConnectionsPerHost(Integer.parseInt(gloCfg.get(CassandraConfig.CORE_CONN_PER_HOST)));
        } catch (Exception ex) {
            log.error(CassandraConfig.CORE_CONN_PER_HOST + " is invalid format");
            log.info("set default " + CassandraConfig.CORE_CONN_PER_HOST + ": 10");
            cfg.setCoreConnectionsPerHost(10);
        }
        
        try {
            cfg.setMaxConnectionPerHost(Integer.parseInt(gloCfg.get(CassandraConfig.MAX_CONN_PER_HOST)));
        } catch (Exception ex) {
            log.error(CassandraConfig.MAX_CONN_PER_HOST + " is invalid format");
            log.info("set default " + CassandraConfig.MAX_CONN_PER_HOST + ": 20");
            cfg.setMaxConnectionPerHost(20);
        }
        
        try {
            cfg.setMaxSimultaneousRequests(Integer.parseInt(gloCfg.get(CassandraConfig.MAX_SIMUL_PER_HOST)));
        } catch (Exception ex) {
            log.error(CassandraConfig.MAX_SIMUL_PER_HOST + " is invalid format");
            log.info("set default " + CassandraConfig.MAX_SIMUL_PER_HOST + ": 20");
            cfg.setMaxSimultaneousRequests(20);
        }
        
        try {
            cfg.setMinSimultaneousRequests(Integer.parseInt(gloCfg.get(CassandraConfig.MIN_SIMUL_PER_HOST)));
        } catch (Exception ex) {
            log.error(CassandraConfig.MIN_SIMUL_PER_HOST + " is invalid format");
            log.info("set default " + CassandraConfig.MIN_SIMUL_PER_HOST + ": 10");
            cfg.setMinSimultaneousRequests(10);
        }
        
        try {
            cfg.setKeepAlive(Boolean.parseBoolean(gloCfg.get(CassandraConfig.KEEP_ALIVE)));
        } catch (Exception ex) {
            log.error(CassandraConfig.KEEP_ALIVE + " is invalid format");
            log.info("set default " + CassandraConfig.KEEP_ALIVE + ": true");
            cfg.setKeepAlive(true);
        }
        
        try {
            cfg.setConnectTimeoutMillis(Integer.parseInt(gloCfg.get(CassandraConfig.CONN_TIMEOUT)));
        } catch (Exception ex) {
            log.error(CassandraConfig.CONN_TIMEOUT + " is invalid format");
            log.info("set default " + CassandraConfig.CONN_TIMEOUT + ": 30000 miliseconds");
            cfg.setConnectTimeoutMillis(30000);
        }
        
        try {
            cfg.setReadTimeoutMillis(Integer.parseInt(gloCfg.get(CassandraConfig.READ_TIMEOUT)));
        } catch (Exception ex) {
            log.error(CassandraConfig.READ_TIMEOUT + " is invalid format");
            log.info("set default " + CassandraConfig.READ_TIMEOUT + ": 30000 miliseconds");
            cfg.setReadTimeoutMillis(30000);
        }
        
        return cfg;

    }

}
